#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys

g_zm_dict = []

''' zmconvert.py table.txt input.txt '''

def build_zm_dict():
    table = open(sys.argv[1], 'rt')
    for line in table.readlines():
        en, cn, frq = line.split(' ')
        if len(cn) == 3:
            g_zm_dict.append([en, unicode(cn, 'utf-8')])

    table.close()

def run():
    input = open(sys.argv[2], 'rt')
    words = input.read().replace(' ', '').replace('\t', '').replace('\r', '').replace('\n', '').decode('utf-8')

    if not g_zm_dict:
        build_zm_dict()

    for w in words:
        codes = ''
        for pair in g_zm_dict:
            en, cn = pair

            if w == cn:
                codes += ',' + en

        if codes:
            print codes[1:], ' ', w.encode('utf-8')

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print 'zmconvert.py table.txt input.txt'
        print 'table.txt and input.txt MUST be utf-8 encoded'
        return

    run()